package com.geso.code_generator.service;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.geso.code_generator.model.ColumnInfo;
import com.geso.code_generator.model.TableInfo;
import com.geso.code_generator.utils.Freemarker;
import com.geso.code_generator.utils.GeneratorUtils;
import com.geso.code_generator.utils.JdbcQuery;
import com.geso.code_generator.utils.PropertiesUtils;

public class GeneratorService {
	
	

	private static final String PACKAGE_NAME = PropertiesUtils.getString("package.name");
	private static final String DEST_PATH = PropertiesUtils.getString("dest.path");
	
	private static String FTL_TEMPLATE_PATH = "";
	private static String PACKAGE_PATH = "";
	
	static {
		String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();
		FTL_TEMPLATE_PATH = path + "ftl";
		PACKAGE_PATH = "src/" + PACKAGE_NAME.replace(".", File.separator);
	}
	
	public static void generateAllCode(){
		
		List<TableInfo> tables = JdbcQuery.getTables();
		for (TableInfo tableInfo : tables) {
			generator(tableInfo);
		}
	}
	
	
	public static void main(String[] args) {
//		generateAllCode();
		TableInfo info = new TableInfo("child_question", "子问题信息");
		generator(info);
	}
	
	public static void generator(TableInfo table){
		Map<String,Object> map = new HashMap<String,Object>();
		String tableName = table.getTableName();
		String objectName = GeneratorUtils.table2ObjectName(tableName);
		map.put("package", PACKAGE_NAME);
		map.put("tableName", tableName);
		map.put("objectName", objectName);
		map.put("objectComment", table.getTableComment());
		List<ColumnInfo> fieldList = JdbcQuery.getTablesColumns(tableName);
		map.put("fieldList", fieldList);
		map.put("columnSize", fieldList.size());
		try {
			Freemarker.printFile("ObjectMapperTemplate.ftl", map, "mybatis/mapper/"+objectName+"Mapper.xml", DEST_PATH, FTL_TEMPLATE_PATH);
			Freemarker.printFile("BaseModel.ftl", map,  PACKAGE_PATH + "/model/base/BaseModel.java", DEST_PATH, FTL_TEMPLATE_PATH);
			Freemarker.printFile("ObjectTemplate.ftl", map,  PACKAGE_PATH + "/model/" + objectName+".java", DEST_PATH, FTL_TEMPLATE_PATH);
			Freemarker.printFile("BaseDao.ftl", map,  PACKAGE_PATH + "/dao/base/BaseDao.java", DEST_PATH, FTL_TEMPLATE_PATH);
			Freemarker.printFile("GeneralWriteDao.ftl", map,  PACKAGE_PATH + "/dao/base/GeneralWriteDao.java", DEST_PATH, FTL_TEMPLATE_PATH);
			Freemarker.printFile("BaseService.ftl", map,  PACKAGE_PATH + "/service/base/BaseService.java", DEST_PATH, FTL_TEMPLATE_PATH);
			Freemarker.printFile("ObjectDao.ftl", map,  PACKAGE_PATH + "/dao/" + objectName+"Dao.java", DEST_PATH, FTL_TEMPLATE_PATH);
			Freemarker.printFile("ObjectService.ftl", map,  PACKAGE_PATH + "/service/" + objectName+"Service.java", DEST_PATH, FTL_TEMPLATE_PATH);
			Freemarker.printFile("ObjectCtrl.ftl", map,  PACKAGE_PATH + "/ctrl/" + objectName+"Ctrl.java", DEST_PATH, FTL_TEMPLATE_PATH);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
